package cn.jingyuan.fox.module.core;

import lombok.Getter;
import lombok.ToString;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * 响应对象
 */
@Getter
@ToString
@Accessors(chain = true)
public class Result<T> implements Serializable {

    private Boolean success;

    private Integer code;

    private String message;

    private T data;

    /**
     * 构造实例
     * <pre>
     *     success=true
     *     code=200
     *     message=null
     *     data=null
     * </pre>
     *
     * @param <T> data数据类型
     */
    public static <T> Result<T> success() {
        return new Result<T>()
            .setSuccess(true)
            .setCode(200);
    }

    /**
     * 构造实例
     * <pre>
     *     success=true
     *     code=200
     *     message=null
     * </pre>
     *
     * @param <T> data数据类型
     */
    public static <T> Result<T> success(T data) {
        return new Result<T>()
            .setSuccess(true)
            .setCode(200)
            .setData(data);
    }

    /**
     * 构造实例
     * <pre>
     *     success=true
     *     code=200
     *     message=message
     * </pre>
     *
     * @param <T> data数据类型
     */
    public static <T> Result<T> success(String message, T data) {
        return new Result<T>()
            .setSuccess(true)
            .setCode(200)
            .setMessage(message)
            .setData(data);
    }

    /**
     * 构造实例
     * <pre>
     *     success=false
     *     code=code
     *     message=message
     * </pre>
     *
     * @param <T> data数据类型
     */
    public static <T> Result<T> fail(Integer code, String message) {
        return new Result<T>()
            .setSuccess(false)
            .setCode(code)
            .setMessage(message);
    }

    /**
     * 构造实例
     * <pre>
     *     success=false
     *     code=code
     *     message=message
     *     data=data
     * </pre>
     *
     * @param <T> data数据类型
     */
    public static <T> Result<T> fail(Integer code, String message, T data) {
        return new Result<T>()
            .setSuccess(false)
            .setCode(code)
            .setMessage(message)
            .setData(data);
    }

    /**
     * 构造实例 - 手动控制
     *
     * @param success 成功状态：true-成功 ， false-失败。
     * @param code 状态码
     * @param message 消息
     * @param data 数据
     * @param <T> data数据类型
     */
    public static <T> Result<T> instance(Boolean success, Integer code, String message, T data) {
        return new Result<T>()
            .setSuccess(success)
            .setCode(code)
            .setMessage(message)
            .setData(data);
    }

    public Result<T> setSuccess(Boolean success) {
        this.success = success;
        return this;
    }

    public Result<T> setCode(Integer code) {
        this.code = code;
        return this;
    }

    public Result<T> setMessage(String message) {
        this.message = message;
        return this;
    }

    public Result<T> setData(T data) {
        this.data = data;
        return this;
    }

    public boolean isSuccess() {
        return Boolean.TRUE.equals(success);
    }

    public boolean unSuccess() {
        return !isSuccess();
    }

}
